Use of the local storage localstorage and its encapsulated interface Store.js 您所在的位置:网站首页 js cookie localstorage Use of the local storage localstorage and its encapsulated interface Store.js

Use of the local storage localstorage and its encapsulated interface Store.js

#Use of the local storage localstorage and its encapsulated interface Store.js| 来源: 网络整理| 查看: 265

Use of the local storage localstorage and its encapsulated interface Store.js Last Update:2017-09-19 Source: Internet Author: User Tags sessionstorage xquery Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more >

Local Storage Localstorage

localstorageHTML5 is a 客户端 new method of storing data that is stored in the client, and the data is 永久保存 , unless human intervention is removed.

localstorageUsed as local storage solves the problem of cookie insufficient storage space: Each cookie has a storage space of cookie 4k, but localStorage the storage space is 5 m in size. In addition, compared to cookies localStorage , 节约带宽 in the same domain, every time the browser sends a request to the server, HTTP will take a cookie, allowing the cookie to be passed back and forth between the browser and the server, wasting bandwidth, However, Localstorage stores the data of the first request directly to the local and avoids passing back and forth.

The limitations of Localstorage1, only the higher version of the browser is supported Localstorage2. The type of the Localstorage value is limited to type string, and JSON conversion is required when used3, if the storage of excessive content will consume memory space, resulting in the page card, because Localstorage is essentially a string read

There are two ways of Localstorage: the localstorage and sessionStorage . sessionStoragemethod 针对一个 session for data storage. When the user closes the browser window, the data is deleted. The only difference between Localstorage and Sessionstorage is that localstorage belongs to persistent storage, and sessionstorage key-value pairs in sessionstorage are emptied when the session ends.

The use of LocalstorageWhen we use localstorage, we need to first determine whether the browser supports localstorage this property:

if(window.localStorage){ alert("浏览器支持localStorage"); }else{ alert("浏览器支持localStorage"); }?

The next is its write, read, delete

Write to Localstoragevar storage=Window.localstorage; storage["A"]=1;Write a field storage.b=2;Write B field Storage.setitem ("C", 3);Write to the C field console.Logtypeof storage["a"]);String Console.Logtypeof storage[ "B"]); //string console. log (typeof storage[ "C"]);? //string //localstorage read var A =storage.a; Console. log (a); //1var b=storage[ "B"]; console.< Span class= "hljs-built_in" >log (b); //2var c=storage.getitem ( "C"); Console. log (c);? //3//localstorage delete storage. Clear (); //clear all contents of Localstorage

Use the key () method to get the corresponding key

var storage=window.localStorage; storage.a=1; storage.setItem("c",3); for(var i=0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有